package com.hitcoder.chat.emoji

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.AdapterView.OnItemClickListener
import com.hitcoder.chat.R
import com.hitcoder.chat.utils.dip2px
import java.util.*

/**
 * 编码参考 http://www.oicqzone.com/tool/emoji/
 */
@SuppressLint("ViewConstructor")
class EmotionView(context: Context?, private val mEditText: EditText) : GridView(context) {

    private val deleteButton = "deleteButton"

    companion object {
        private var sNumColumns = 0
        private var sNumRows = 0
        private var sPadding = 0
        private var sEmotionSize = 0

        @JvmStatic
        fun calSizeForContainEmotion(context: Context, width: Int, height: Int): Int {
            sPadding = 5f.dip2px(context)
            sEmotionSize = 50f.dip2px(context)
            sNumColumns = width / sEmotionSize
            sNumRows = height / sEmotionSize
            return sNumColumns * sNumRows
        }
    }

    fun buildEmotions(data: List<Emotion>, delBtn: Boolean) {

        val realData: MutableList<Emotion> = ArrayList(sNumColumns * sNumRows)
        realData.addAll(data)

        if (delBtn) {
            realData.add(Emotion(deleteButton, R.mipmap.icon_del))
        }

        numColumns = sNumColumns

        setPadding(sPadding, sPadding, sPadding, sPadding)

        clipToPadding = false

        adapter = EmotionAdapter(context, realData)

        onItemClickListener =
            OnItemClickListener { parent: AdapterView<*>, _: View, position: Int, _: Long ->
                val emotion =
                    parent.adapter.getItem(position) as? Emotion ?: return@OnItemClickListener

                if (TextUtils.equals(emotion.text, deleteButton)) {
                    delClick()
                    return@OnItemClickListener
                }

                val start = mEditText.selectionStart
                val editable = mEditText.editableText
                val emotionSpannable = EmojiSpanBuilder.buildEmotionSpannable(context, emotion.text)
                editable.insert(start, emotionSpannable)
            }
    }

    private fun delClick() {
        val action = KeyEvent.ACTION_DOWN
        val code = KeyEvent.KEYCODE_DEL
        val event = KeyEvent(action, code)
        mEditText.onKeyDown(KeyEvent.KEYCODE_DEL, event)
    }

    class EmotionAdapter(private val context: Context, private val emotions: List<Emotion>) :
        BaseAdapter() {

        override fun getView(position: Int, view: View?, parent: ViewGroup): View {
            var view1 = view
            if (view1 == null) {
                view1 = LayoutInflater.from(context)
                    .inflate(R.layout.vh_emotion_item_layout, parent, false)
            }
            val imageView = view1?.findViewById<ImageView>(R.id.image)
            imageView?.setImageResource((getItem(position) as Emotion).drawableRes)
            return view1!!
        }

        override fun getCount(): Int {
            return emotions.size
        }

        override fun getItem(position: Int): Any {
            return emotions[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

    }


}